package system;

import java.awt.Dimension;
import java.awt.event.KeyEvent;

import javax.swing.Box;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import controllers.ClerkMenuController;
import controllers.CustomerMenuController;
import controllers.DeleteTupleController;
import controllers.DisplayTableController;
import controllers.InsertTupleController;
import controllers.AbstractController;
import controllers.ManagerMenuController;

@SuppressWarnings("serial")
public class 
MenuBar 
extends JMenuBar
{	private JMenu insertTuple;
	private JMenu deleteTuple;
	private JMenu displayTable;
	private JMenu customerMenu;
	private JMenu clerkMenu;
	private JMenu managerMenu;
	 
	private static int NO_KEY_EVENT = 0;
	
	MenuBar()
	{ 	// indent first menu
		add(Box.createRigidArea(new Dimension(10,0)));
	
		setupInsertTupleMenu();
		setupDeleteTupleMenu();
		setupDisplayTableMenu();
		setupCustomerMenu();
		setupClerkMenu();
		setupManagerMenu();
	}

	private void setupManagerMenu()
	{
		managerMenu = new JMenu("Manager Menu");
		createMenuItem(managerMenu, "Add Supplier", NO_KEY_EVENT, "Insert Supplier");
		createMenuItem(managerMenu, "Remove Supplier", NO_KEY_EVENT, "Delete Supplier");
		createMenuItem(managerMenu, "Process Shipment", NO_KEY_EVENT, "Process Shipment");
		createMenuItem(managerMenu, "Process Delivery", NO_KEY_EVENT, "Process Delivery");
		createMenuItem(managerMenu, "Show Top Selling Items", NO_KEY_EVENT, "Top Selling Items");
		createMenuItem(managerMenu, "Generate Daily Sales Report", NO_KEY_EVENT, "Daily Sales Report");
		add(managerMenu);
	}
	private void setupCustomerMenu()
	{
		customerMenu = new JMenu("Customer Menu");
		createMenuItem(customerMenu, "Register New Customer", NO_KEY_EVENT, "Register");
		createMenuItem(customerMenu, "Existing Customer Login", NO_KEY_EVENT, "Login");
		createMenuItem(customerMenu, "Purchase Items", NO_KEY_EVENT, "Purchase");
		createMenuItem(customerMenu, "Search Items", NO_KEY_EVENT,"Search");
		add(customerMenu);
	}
	
	private void setupClerkMenu()
	{
		clerkMenu = new JMenu("Clerk Menu");
		createMenuItem(clerkMenu, "Process Purchase", NO_KEY_EVENT, "Process Purchase");
		createMenuItem(clerkMenu, "Process Return", NO_KEY_EVENT, "Process Return");
		add(clerkMenu);
	}
	
	private void 
	setupDisplayTableMenu() 
	{	 displayTable = new JMenu("Display Tables");
		
		createMenuItem(
			displayTable, "Display Items...", NO_KEY_EVENT, "Display Items"
		);
		createMenuItem(
			displayTable, "Display Lead Singers/Products...", 
			NO_KEY_EVENT, "Display Lead Singers/Products"
		);
		createMenuItem(
			displayTable, "Display Songs/Products...", 
			NO_KEY_EVENT, "Display Songs/Products"
		);
		createMenuItem(
			displayTable, "Display Suppliers...", 
			NO_KEY_EVENT, "Display Suppliers"
		);
		createMenuItem(
			displayTable, "Display Shipments...", 
			NO_KEY_EVENT, "Display Shipments"
		);
		createMenuItem(
			displayTable, "Display Items in Shipments...", 
			NO_KEY_EVENT, "Display Items in Shipments"
		);
		createMenuItem(
			displayTable, "Display Stores...", NO_KEY_EVENT, "Display Stores"
		);
		createMenuItem(
			displayTable, "Display Items in Stores...", 
			NO_KEY_EVENT, "Display Items in Stores"
		);
		createMenuItem(
			displayTable, "Display Purchases...", 
			NO_KEY_EVENT, "Display Purchases"
		);
		createMenuItem(
			displayTable, "Display Purchased Items...", 
			NO_KEY_EVENT, "Display Purchased Items"
		);
		createMenuItem(
			displayTable, "Display Customers...", 
			NO_KEY_EVENT, "Display Customers"
		);
		createMenuItem(
			displayTable, "Display Returns...", 
			NO_KEY_EVENT, "Display Returns"
		);
		createMenuItem(
			displayTable, "Display Returned Items...", 
			NO_KEY_EVENT, "Display Returned Items"
		);
		
		add(displayTable);
	}
	
	private void 
	setupDeleteTupleMenu() 
	{	deleteTuple = new JMenu("Delete Tuple");
	
		createMenuItem(
			deleteTuple, "Delete Item...", NO_KEY_EVENT, "Delete Item"
		);
		createMenuItem(
			deleteTuple, "Dissasociate Lead Singer...", 
			NO_KEY_EVENT, "Dissasociate Lead Singer"
		);
		createMenuItem(
			deleteTuple, "Dissasociate Song...", 
			NO_KEY_EVENT, "Dissasociate Song"
		);
		createMenuItem(
			deleteTuple, "Delete Supplier...", NO_KEY_EVENT, "Delete Supplier"
		);
		createMenuItem(
			deleteTuple, "Delete Shipment...", NO_KEY_EVENT, "Delete Shipment"
		);
		createMenuItem(
			deleteTuple, "Remove Item from Shipment...", 
			NO_KEY_EVENT, "Remove Item from Shipment"
		);
		createMenuItem(
			deleteTuple, "Delete Store...", NO_KEY_EVENT, "Delete Store"
		);
		createMenuItem(
			deleteTuple, "Remove Item from Store...", 
			NO_KEY_EVENT, "Remove Item from Store"
		);
		createMenuItem(
			deleteTuple, "Delete Purchase...", NO_KEY_EVENT, "Delete Purchase"
		);
		createMenuItem(
			deleteTuple, "Delete Item from Purchased...", 
			NO_KEY_EVENT, "Delete Item from Purchased"
		);
		createMenuItem(
			deleteTuple, "Delete Customer...", NO_KEY_EVENT, "Delete Customer"
		);
		createMenuItem(
			deleteTuple, "Delete Return...", NO_KEY_EVENT, "Delete Return"
		);
		createMenuItem(
			deleteTuple, "Remove Item from Returns...", 
			NO_KEY_EVENT, "Remove Item from Returns"
		);
		
		add(deleteTuple);
	}

	private void 
	setupInsertTupleMenu()
	{	insertTuple = new JMenu("Insert Tuple");
		
		// when alt-b is pressed on the keyboard, the menu will appear
		insertTuple.setMnemonic(KeyEvent.VK_B);
	
		createMenuItem(
			insertTuple, "Insert Item...", NO_KEY_EVENT, "Insert Item"
		);
		createMenuItem(
			insertTuple, "Set Lead Singer...", NO_KEY_EVENT, "Set Lead Singer"
		);
		createMenuItem(
			insertTuple, "Set Song...", NO_KEY_EVENT, "Set Song"
		);
		createMenuItem(
			insertTuple, "Insert Supplier...", NO_KEY_EVENT, "Insert Supplier"
		);
		createMenuItem(
			insertTuple, "Insert Shipment...", NO_KEY_EVENT, "Insert Shipment"
		);
		createMenuItem(
			insertTuple, "Add Item to Shipment...", 
			NO_KEY_EVENT, "Add Item to Shipment"
		);
		createMenuItem(
			insertTuple, "Insert Store...", NO_KEY_EVENT, "Insert Store"
		);
		createMenuItem(
			insertTuple, "Add Item to Store...", 
			NO_KEY_EVENT, "Add Item to Store"
		);
		createMenuItem(
			insertTuple, "Insert Purchase...", NO_KEY_EVENT, "Insert Purchase"
		);
		createMenuItem(
			insertTuple, "Set Item as Purchased...", 
			NO_KEY_EVENT, "Set Item as Purchased"
		);
		createMenuItem(
			insertTuple, "Insert Customer...", NO_KEY_EVENT, "Insert Customer"
		);
		createMenuItem(
			insertTuple, "Insert Return...", NO_KEY_EVENT, "Insert Return"
		);
		createMenuItem(
			insertTuple,"Add Item to Returns...", 
			NO_KEY_EVENT, "Add Item to Returns"
		);
		
		add(insertTuple);
	}
	
	private JMenuItem 
	createMenuItem(JMenu menu, String label, int key, String actionCommand)
	 {  JMenuItem menuItem = new JMenuItem(label);
		
		if (key > 0) 
			menuItem.setMnemonic(key);
		if (actionCommand.length() > 0) 
			menuItem.setActionCommand(actionCommand);
		menu.add(menuItem);
	
		return menuItem;
	 }
	
	public void 
	registerControllers(View v)
	{	registerController(new InsertTupleController(v), insertTuple);
		registerController(new DeleteTupleController(v), deleteTuple);
		registerController(new DisplayTableController(v), displayTable);
		registerController(new CustomerMenuController(v), customerMenu);
		registerController(new ClerkMenuController(v), clerkMenu);
		registerController(new ManagerMenuController(v), managerMenu);
	 }

	private void 
	registerController(AbstractController controller, JMenu m)
	{	for(int i = 0; i < m.getItemCount(); i++)
		    m.getItem(i).addActionListener(controller);
	}
}
